/**
 * @author yinyuejia
 * @file localstorage 相关设置
 */

// STORAGE_KEY为空，多key模式
const STORAGE_KEY = '';
const localstorage = {
  set(key: any, value: any, object?: any, callback?: any) {
    const keys = key.split('.').filter((m: any) => m !== '');
    if (STORAGE_KEY.length > 0) {
      const current = object || JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '{}');
      const cb = () => {
        window.localStorage.setItem(STORAGE_KEY, JSON.stringify(current));
      };
      if (current !== undefined && current !== null && keys.length > 0) {
        const firstKey = keys.shift();
        if (keys.length > 0) {
          if (current[firstKey] === undefined) {
            current[firstKey] = {};
          }
          this.set(keys.join('.'), value, current[firstKey], callback || cb);
          return;
        }
        current[firstKey] = value;
      } else {
        current[key] = value;
      }
      callback ? callback() : cb();
    } else if (keys.length > 0) {
      let firstKey = keys.shift();
      const storeKey = firstKey;
      let current = object;
      let currentValue: any = null;
      let skip = false;
      if (current) {
        currentValue = current[firstKey];
      } else {
        current = JSON.parse(window.localStorage.getItem(firstKey) || '{}');
        if (keys.length > 0) {
          firstKey = keys.shift();
          currentValue = current[firstKey];
        } else {
          current = value;
          skip = true;
        }
      }
      const cb = () => {
        window.localStorage.setItem(storeKey, JSON.stringify(current));
      };
      if (keys.length > 0) {
        if (currentValue === undefined) {
          currentValue = {};
        }
        this.set(keys.join('.'), value, currentValue, callback || cb);
        return;
      }
      skip ? '' : (current[firstKey] = value);

      callback ? callback() : cb();
    }
  },
  // @ts-ignore
  get(key: any, object?: any) {
    const keys = key.split('.').filter((m: any) => m !== '');
    if (STORAGE_KEY.length > 0) {
      const current = object || JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '{}');
      if (current !== undefined && current !== null && keys.length > 0) {
        const firstKey = keys.shift();
        if (keys.length > 0) {
          return this.get(keys.join('.'), current[firstKey]);
        }
        return current[key];
      }
      return undefined;
    }
    if (keys.length > 0) {
      const firstKey = keys.shift();
      let current;
      let currentValue;
      if (object) {
        current = object;
        currentValue = object[firstKey];
      } else {
        current = JSON.parse(window.localStorage.getItem(firstKey) || '{}');
        currentValue = current;
      }
      if (keys.length > 0) {
        return this.get(keys.join('.'), current);
      }
      return currentValue;
    }
    return undefined;
  },
  clear(key: any) {
    if (key === undefined) {
      if (STORAGE_KEY.length > 0) {
        window.localStorage.removeItem(STORAGE_KEY);
      } else {
        window.localStorage.clear();
      }
    } else if (STORAGE_KEY.length > 0) {
      const current = this.get(key);
      if (current !== undefined && current !== null) {
        this.set(key, undefined);
      }
    } else {
      window.localStorage.removeItem(key);
    }
  },
};
export default localstorage;
